'''
1、解析网页：bs4 
2、语言：python3
3、框架：不选用
'''
from bs4 import BeautifulSoup
from urllib import request
import chardet
import sys
sys.path.append('E:\\TJ-study\\python3\\Frame\\flask\\weather')
from utils import strUtils
from weather import settings

CITY_MIN_LENGTH = getattr(settings, 'CITY_MIN_LENGTH', 8)


class CityCode(object):

    def __init__(self):
        self.charset = "utf-8"

    def login(self):
        '''对于需要登录的网页，获取token'''
        pass

    def randomHeader(self):
        '''模拟用户请求，防止被封'''
        pass

    @property
    def htmlContext(self):
        '''根据url获取html'''
        # https://www.cnblogs.com/oucbl/p/6138963.html
        html = request.urlopen('https://www.cnblogs.com/oucbl/p/6138963.html').read()
        self.charset = chardet.detect(html).get("encoding", "utf-8")
        return html.decode(self.charset)

    def parseHtml(self):
        '''解析网页，获取数据'''
        soup = BeautifulSoup(self.htmlContext, 'html.parser')
        res_data = soup.find_all('pre', class_='xiaoshujiang_code_container')
        data_list = [strUtils.multilineToList(data.string, CITY_MIN_LENGTH) for data in res_data[1:]]

        with open('cityCode.txt', 'w+') as f:
            for data in data_list:
                for city in data:
                    f.write("%s\n" % city)
                f.write(" \n")
        

    def resultAdapter(self):
        '''
        格式化返回的结果：
            json、xml、text等
        '''
        pass


if __name__ == "__main__":
    
    city = CityCode()
    city.parseHtml()